package com.ccp.dev.extend.util;

import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.StringPool;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.extend.model.ExtendBaseModuleInfo;
import com.ccp.dev.extend.model.ExtendColumnInfo;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zzy
 * @Description: 迁移平台中的工具,并作出修改.
 */
public class ExtendGenUtils {

    /**
     * java类型转换
     */
    public static Map<String, String> javaTypeMap = new HashMap<String, String>();

    /**
     * jdbc类型转换
     */
    public static Map<String, String> jdbcTypeMap = new HashMap<String, String>();
    /**
     * 模板后缀名称
     */
    public static final String templateSuffixName = ".ftl";

    /** 字段数,用于判断要生成的模板样式 */
    public static final int FIELDSNUM = 15;

    /**
     * 设置列信息
     */
    public static List<ExtendColumnInfo> columnsInfoOfSetting(List<ExtendColumnInfo> columns) {
        // 列信息
        List<ExtendColumnInfo> columsList = new ArrayList<>();
        for (ExtendColumnInfo column : columns) {
            if(StringUtil.isEmpty(column.getJdbcType())){
                column.setJdbcType(jdbcTypeMap.get(column.getDataType()));
            }
            // 列名转换成Java属性名
            String attrName=column.getAttrNameForProperty();
            if(StringUtil.isEmpty(attrName)){
                //属性名称为空，说明前端页面没有传值过来，默认根据表字段名称生成
                attrName = StringUtil.convertToCamelCase(column.getColumnName());
            }
            //转换为首字母大写 如果有_则转换为驼峰
            attrName = StringUtil.convertToCamelCase(column.getAttrNameForProperty());

            // todo 这里不明白什么意思,暂时注释掉 2020-9-16
            //若第二个字符为大写,则get,set方法将第一个字符转换为小写
            //if(attrName.length()>1){
            //    char charName  =attrName.charAt(1);
            //    if(Character.isUpperCase(charName)){
            //        attrName = StringUtil.convertToCamelFirstLowerCase(attrName);
            //    }else{
            //        attrName = StringUtil.makeFirstLetterUpperCase(attrName);
            //    }
            //}

            column.setAttrNameForMethod(attrName);
            // 转换首字母小写,用于填充属性名
            column.setAttrNameForProperty(StringUtils.uncapitalize(attrName));

            if("tenantid".equals(column.getColumnName().toLowerCase())){
                column.setAttrNameForProperty("tenantId");
            }
            if("createby".equals(column.getColumnName().toLowerCase())){
                column.setAttrNameForProperty("createBy");
            }
            if("createtime".equals(column.getColumnName().toLowerCase())){
                column.setAttrNameForProperty("createTime");
            }
            if("updateby".equals(column.getColumnName().toLowerCase())){
                column.setAttrNameForProperty("updateBy");
            }
            if("updatetime".equals(column.getColumnName().toLowerCase())){
                column.setAttrNameForProperty("updateTime");
            }
            // 列的数据类型，转换成Java类型
            String attrType =column.getAttrType();
            if(StringUtil.isEmpty(attrType)){
                //java属性类型为空快说名前端页面没有传值过来，默认根据表字段类型生成
                attrType = javaTypeMap.get(column.getDataType());
                if(StringUtils.isEmpty(attrType)){
                    attrType = "String";
                }
                column.setAttrType(attrType.trim());
            }
            //若列注释为空则显示字段名称
            if(StringUtil.isEmpty(column.getColumnComment())){
                column.setColumnComment(column.getAttrNameForProperty());
            }
            String jdbcType = jdbcTypeMap.get(column.getDataType());
            if(StringUtils.isEmpty(jdbcType)){
                jdbcType = "VARCHAR";
            }
            column.setJdbcType(jdbcType);
            columsList.add(column);
        }
        return columsList;
    }

    /**
     * 获取模板信息
     *
     * @return 模板列表
     */
    public static List<String> getTemplates(ExtendBaseModuleInfo baseModuleInfo,String selectFlow) {
        String templateName = "codeGen/ftls";
        List<String> templates = new ArrayList<>();
        templates.add( templateName + StringPool.SLASH + "java/Model.java" + templateSuffixName);
        templates.add( templateName + StringPool.SLASH + "java/Dao.java" + templateSuffixName);
        templates.add( templateName + StringPool.SLASH + "java/Service.java" + templateSuffixName);
        templates.add( templateName + StringPool.SLASH + "java/Controller.java" + templateSuffixName);
        templates.add( templateName + StringPool.SLASH + "xml/Mapper.xml" + templateSuffixName);

        int result = 0;
        if(BeanUtils.isNotEmpty(baseModuleInfo.getShowColumns())){
            result = baseModuleInfo.getShowColumns().size();
        }else{
            result = baseModuleInfo.getColumns().size();
        }

        //小于=15的使用两列布局 大于15的使用六列布局
        if (result<=FIELDSNUM) {
            templates.add( templateName + StringPool.SLASH + "html/add2Col.html" + templateSuffixName);
            templates.add( templateName + StringPool.SLASH + "html/particulars2Col.html" + templateSuffixName);
        }
        else if(result>FIELDSNUM){
            templates.add( templateName + StringPool.SLASH + "html/add6Col.html" + templateSuffixName);
            templates.add( templateName + StringPool.SLASH + "html/particulars6Col.html" + templateSuffixName);
        }

        templates.add( templateName + StringPool.SLASH + "html/import.html" + templateSuffixName);
        templates.add( templateName + StringPool.SLASH + "html/list.html" + templateSuffixName);
        //if(baseModuleInfo.getShowFull()){
        //    templates.add( templateName + StringPool.SLASH + "html/iframe.html" + templateSuffixName);
        //}
        if(StringUtils.isNotEmpty(selectFlow)){
            templates.add( templateName + StringPool.SLASH + "html/modify.html" + templateSuffixName);
            templates.add( templateName + StringPool.SLASH + "html/detail.html" + templateSuffixName);
        }
        return templates;
    }

    /**
     *
     * 功能描述: 获取完整的文件路径
     *
     * @param: baseModuleInfo  模板基本信息
     * @param: fileTye  模板文件类型
     * @return: String  完整的路径
     * @auther: dfz
     * @date: 2019/5/23 9:21
     */
    public static String getFilePath(ExtendBaseModuleInfo baseModuleInfo, String fileTye){
        //基本包路径
        String basePackage = baseModuleInfo.getBasePackage();
        //模块名称
        String modelName  = baseModuleInfo.getModuleName();
        //classname首字母小写的类名
        String classname  = baseModuleInfo.getClassForPrefix();
        //classname首字母大写的类名
        String className = baseModuleInfo.getClassForClassName();

        String basePath = "src/main";
        //java文件路径
        StringBuffer javaPath = new StringBuffer();
        javaPath.append(basePath+"/java");
        javaPath.append("/"+basePackage.replace(".","/" ));
        javaPath.append("/"+modelName);
        //资源路径
        StringBuffer resourcesPath = new StringBuffer();
        resourcesPath.append(basePath+"/resources");

        switch (fileTye){
            case "Controller.java":
                javaPath.append("/controller/"+className+"Controller.java");
                break;
            case "Service.java":
                javaPath.append("/service/"+className+"Service.java");
                break;
            case "Dao.java":
                javaPath.append("/dao/"+className+"Dao.java");
                break;
            case "Model.java":
                javaPath.append("/model/"+className+".java");
                break;
            case "Mapper.xml":
                resourcesPath.append("/mapper/"+className+"Mapper.map.xml");
                break;

            case "import.html":
                resourcesPath.append("/templates/"+classname+"/import.html");
                break;
            case "list.html":
                resourcesPath.append("/templates/"+classname+"/list.html");
                break;

            case "modify.html":
                resourcesPath.append("/templates/"+classname+"/modify.html");
                break;
            case "detail.html":
                resourcesPath.append("/templates/"+classname+"/detail.html");
                break;
            case "add2Col.html":
                resourcesPath.append("/templates/"+classname+"/add.html");
                break;
            case "add6Col.html":
                resourcesPath.append("/templates/"+classname+"/add.html");
                break;
            case "particulars2Col.html":
                resourcesPath.append("/templates/"+classname+"/particulars.html");
                break;
            case "particulars6Col.html":
                resourcesPath.append("/templates/"+classname+"/particulars.html");
                break;
            default:
                break;
        }
        if(fileTye.endsWith(".java")){
            //java文件
            return  javaPath.toString();
        }else {
            //资源文件
            return resourcesPath.toString();
        }
    }

    static {
        /**
         * 数据库字段类型和java类型映射
         */
        javaTypeMap.put("tinyint", "Integer");
        javaTypeMap.put("smallint", "Integer");
        javaTypeMap.put("mediumint", "Integer");
        javaTypeMap.put("int", "Integer");
        javaTypeMap.put("number", "Long");
        javaTypeMap.put("integer", "Integer");
        javaTypeMap.put("bigint", "Long");
        javaTypeMap.put("float", "Float");
        javaTypeMap.put("double", "Double");
        javaTypeMap.put("decimal", "BigDecimal");
        javaTypeMap.put("bit", "Boolean");
        javaTypeMap.put("char", "String");
        javaTypeMap.put("varchar", "String");
        javaTypeMap.put("varchar2", "String");
        javaTypeMap.put("nvarchar2", "String");
        javaTypeMap.put("tinytext", "String");
        javaTypeMap.put("text", "String");
        javaTypeMap.put("mediumtext", "String");
        javaTypeMap.put("longtext", "String");
        javaTypeMap.put("time", "Date");
        javaTypeMap.put("date", "Date");
        javaTypeMap.put("datetime", "Date");
        javaTypeMap.put("timestamp", "Date");
        javaTypeMap.put("timestamp(6)", "Date");
        javaTypeMap.put("clob", "String");
        javaTypeMap.put("blob", "byte[]");
        javaTypeMap.put("int8", "Long");
        javaTypeMap.put("int4", "Integer");
        javaTypeMap.put("int2", "Integer");
        javaTypeMap.put("numeric", "Long");
    }

    static {
        /**
         * 数据库类型和mybatis的jdbcType类型映射
         */
        jdbcTypeMap.put("char", "CHAR");
        jdbcTypeMap.put("varchar", "VARCHAR");
        jdbcTypeMap.put("varchar2", "VARCHAR");
        jdbcTypeMap.put("nvarchar2", "VARCHAR");
        jdbcTypeMap.put("bit", "BOOLEAN");
        jdbcTypeMap.put("tinyint", "TINYINT");
        jdbcTypeMap.put("smallint", "SMALLINT");
        jdbcTypeMap.put("int", "INTEGER");
        jdbcTypeMap.put("bigint", "BIGINT");
        jdbcTypeMap.put("float", "FLOAT");
        jdbcTypeMap.put("double", "DOUBLE");
        jdbcTypeMap.put("time", "TIMESTAMP");
        jdbcTypeMap.put("date", "TIMESTAMP");
        jdbcTypeMap.put("clob", "CLOB");
        jdbcTypeMap.put("blob", "BLOB");
        jdbcTypeMap.put("integer", "INTEGER");
        jdbcTypeMap.put("mediumint", "INTEGER");
        jdbcTypeMap.put("number", "NUMERIC");
        jdbcTypeMap.put("decimal", "NUMERIC");
        jdbcTypeMap.put("timestamp", "TIMESTAMP");
        jdbcTypeMap.put("timestamp(6)", "TIMESTAMP");
        jdbcTypeMap.put("text", "VARCHAR");
        jdbcTypeMap.put("datetime", "TIMESTAMP");
        jdbcTypeMap.put("tinytext", "VARCHAR");
        jdbcTypeMap.put("mediumtext", "VARCHAR");
        jdbcTypeMap.put("longtext", "VARCHAR");
        jdbcTypeMap.put("int8", "NUMERIC");
        jdbcTypeMap.put("int4", "INTEGER");
        jdbcTypeMap.put("int2", "INTEGER");
        jdbcTypeMap.put("numeric", "NUMERIC");
    }


}

